package com.sxpcwlkj.framework.filter;

import com.sxpcwlkj.framework.handler.HttpServletRequestWrapperHandler;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

/**
 * @Description TODO   RequestFilter，请求过滤器
 * @Author sxpcwlkj
 * @Date ${DATE} ${TIME}
 * @Version v1.0.0
 */
@Slf4j
@WebFilter(filterName = "requestFilter", urlPatterns = {"/**"})
@Component
public class RequestFilter implements Filter {


    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {

        log.info("======HttpServletRequestWrapper======");
        HttpServletRequestWrapperHandler myRequest = new HttpServletRequestWrapperHandler((HttpServletRequest) request);
        // 全局请求接口  添加一个参数
        // myRequest.setParameter("parameter", "自定义请求参数");


        Map<String, String[]> parameterMap = myRequest.getParameterMap();
        parameterMap.forEach((key, value) -> System.out.println("Key: " + key + ", Value: " + Arrays.toString(value)));


        chain.doFilter(myRequest, response);

    }

}
